package com.example.demo3.util;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author allnas
 */
@Slf4j
@Component
public class AesUtils {

    @Value("${aes-key}")
    private String key;

    private SymmetricCrypto aes;

    @PostConstruct
    public void init() {
        aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key.getBytes());
    }

    public String encrypt(String content) {
        return aes.encryptHex(content);
    }

    public String decrypt(String content) {
        return aes.decryptStr(content, CharsetUtil.CHARSET_UTF_8);
    }
}
